package com.situ.edu.dao.impl;

import com.situ.edu.dao.IXiTeacherDao;
import com.situ.edu.pojo.entity.Teacher;
import com.situ.edu.pojo.query.TeacherQuery;
import com.situ.edu.pojo.vo.TeacherVO;
import com.situ.edu.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class XiTeacherDaoImpl implements IXiTeacherDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<TeacherVO> selectByPage(TeacherQuery teacherQuery) {
        String sql = "SELECT t.id,t.name,t.gender,t.title,t.`status`,t.xi_id,x.`name` AS xiName,t.create_time,t.update_time\n" +
                "FROM teacher AS t INNER JOIN xi AS x \n" +
                "ON t.xi_id=x.id where 1=1";

        List<Object> queryList = new ArrayList<>();
        //搜索教师名字
        if (!ObjectUtils.isEmpty(teacherQuery.getName())){
            sql += " and t.name like ?";
            queryList.add("%"+teacherQuery.getName()+"%");
        }
        //搜索教师所在系
        if (!ObjectUtils.isEmpty(teacherQuery.getXiId())){
            sql += " and t.xi_id=?";
            queryList.add(teacherQuery.getXiId());
        }
        sql += " limit ?,?";
        int offset = (teacherQuery.getPage() - 1)*teacherQuery.getLimit();
        queryList.add(offset);
        queryList.add(teacherQuery.getLimit());

        return template.query(sql,
                new BeanPropertyRowMapper<>(TeacherVO.class),queryList.toArray());
    }

    @Override
    public int selectTotalCount(TeacherQuery teacherQuery) {
        String sql = "SELECT count(*)\n" +
                "FROM teacher AS t INNER JOIN xi AS x \n" +
                "ON t.xi_id=x.id where 1=1";
        List<Object> queryList = new ArrayList<>();
        //搜索教师名字
        if (!ObjectUtils.isEmpty(teacherQuery.getName())){
            sql += " and t.name like ?";
            queryList.add("%"+teacherQuery.getName()+"%");
        }
        //搜索教师所在系
        if (!ObjectUtils.isEmpty(teacherQuery.getXiId())){
            sql += " and t.xi_id=?";
            queryList.add(teacherQuery.getXiId());
        }
        return template.queryForObject(sql,Integer.class,queryList.toArray());
    }

    @Override
    public void add(Teacher teacher) {
        template.update("insert into teacher (name,gender,title,xi_id) values (?,?,?,?)",
                teacher.getName(),teacher.getGender(),teacher.getTitle(),teacher.getXiId());
    }

    @Override
    public void updateStatus(int id, int status) {
        template.update("update teacher set status=? where id=?",status,id);
    }

    @Override
    public void deleteById(int id) {
        template.update("delete from teacher where id=?",id);
    }

    @Override
    public Teacher selectById(int id) {
        return template.queryForObject("select * from teacher where id=?",
                new BeanPropertyRowMapper<>(Teacher.class),id);
    }

    @Override
    public void update(Teacher teacher) {
        template.update("update teacher set name=?,gender=?,title=?,xi_id=? where id=?",
                teacher.getName(),teacher.getGender(),teacher.getTitle(),teacher.getXiId(),teacher.getId());
    }
}
